package com.homepage.service;

import com.homepage.domain.Featurette;
import com.homepage.repository.FeaturetteRepository;
import com.homepage.service.dto.FeaturetteDTO;
import com.homepage.service.mapper.FeaturetteMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link Featurette}.
 */
@Service
@Transactional
public class FeaturetteService {

    private final Logger log = LoggerFactory.getLogger(FeaturetteService.class);

    private final FeaturetteRepository featuretteRepository;

    private final FeaturetteMapper featuretteMapper;

    public FeaturetteService(FeaturetteRepository featuretteRepository, FeaturetteMapper featuretteMapper) {
        this.featuretteRepository = featuretteRepository;
        this.featuretteMapper = featuretteMapper;
    }

    public List<FeaturetteDTO> getAllByCompanyId(String companyId)
    {
        return featuretteRepository.findAllByCompanyId(companyId).stream()
                .map(featuretteMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Save a featurette.
     *
     * @param featuretteDTO the entity to save.
     * @return the persisted entity.
     */
    public FeaturetteDTO save(FeaturetteDTO featuretteDTO) {
        log.debug("Request to save Featurette : {}", featuretteDTO);
        Featurette featurette = featuretteMapper.toEntity(featuretteDTO);
        featurette = featuretteRepository.save(featurette);
        return featuretteMapper.toDto(featurette);
    }

    public List<FeaturetteDTO> saveAll(List<FeaturetteDTO> featuretteDTOS){
        log.debug("Request to save Featurette : {}", featuretteDTOS);
        List<Featurette> featurettes = featuretteMapper.toEntity(featuretteDTOS);
        featurettes = featuretteRepository.saveAll(featurettes);
        return featuretteMapper.toDto(featurettes);
    }

    /**
     * Partially update a featurette.
     *
     * @param featuretteDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<FeaturetteDTO> partialUpdate(FeaturetteDTO featuretteDTO) {
        log.debug("Request to partially update Featurette : {}", featuretteDTO);

        return featuretteRepository
            .findById(featuretteDTO.getId())
            .map(existingFeaturette -> {
                featuretteMapper.partialUpdate(existingFeaturette, featuretteDTO);

                return existingFeaturette;
            })
            .map(featuretteRepository::save)
            .map(featuretteMapper::toDto);
    }

    /**
     * Get all the featurettes.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<FeaturetteDTO> findAll() {
        log.debug("Request to get all Featurettes");
        return featuretteRepository.findAll().stream().map(featuretteMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     *  Get all the featurettes where FeaturetteImage is {@code null}.
     *  @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<FeaturetteDTO> findAllWhereFeaturetteImageIsNull() {
        log.debug("Request to get all featurettes where FeaturetteImage is null");
        return StreamSupport
            .stream(featuretteRepository.findAll().spliterator(), false)
            .map(featuretteMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one featurette by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<FeaturetteDTO> findOne(Long id) {
        log.debug("Request to get Featurette : {}", id);
        return featuretteRepository.findById(id).map(featuretteMapper::toDto);
    }

    /**
     * Delete the featurette by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete Featurette : {}", id);
        featuretteRepository.deleteById(id);
    }
}
